﻿@*
    ﻿//*********************************************************
    //
    // Copyright (c) Hewlett Packard. All rights reserved.
    // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
    // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
    // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
    // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
    //
    //*********************************************************
*@
@{
    ViewBag.Title = "Create a Record with ACL (JSON)";
}

@section helpDescription {
    <p>While there is a detailed <a href="~/examples/angular/RecordSecurity">example</a> of how to update the ACL of a record this example demonstrates how to create a new record and set its ACL.  All that is required is to compose a valid object for the AccessControlList property and post that along with the new record.  This is only available when posting a JSON object not when using the simple HTML form method of creating a record.</p>
    <h3>Example</h3>
    <p>The following object will create a new record with DestroRecord set to private for the location with Uri 1.</p>
    <pre>{
    "RecordTitle": "test with ACL",
    "RecordRecordType": "2",
    "AccessControlList": {
        "FunctionEnum": "RecordAccess",
        "FunctionProfiles": {
            "DestroyRecord": {
                "Setting": "Private",
                "ReferenceStyle": "NoRefNoCopy",
                "AccessLocations": [
                    {
                        "Uri": 1
                    }
                ]
            }
        }
    }
}</pre>

    <p>It is also possible to use a search string to specify each access location, for example:</p>

    <pre>{
    "RecordTitle": "test with ACL",
    "RecordRecordType": "2",
    "AccessControlList": {
        "FunctionEnum": "RecordAccess",
        "FunctionProfiles": {
            "DestroyRecord": {
                "Setting": "Private",
                "ReferenceStyle": "NoRefNoCopy",
                "AccessLocations": [
                    {
                        "FindBy": "me"
                    }
                ]
            }
        }
    }
}</pre>
    <p>NOTE: The FindBy must return only one location.</p>
}

<form id="recordForm" class="trim-form" method="post" action="~/Record">
    <fieldset>
        <legend>Record details</legend>

        <label>Title:</label>
        <input type="text" name="RecordTitle" value="" />

        <label>Type:</label>
        <input type="text" name="RecordRecordType" value="" class="typeahead" data-trim-type="RecordType" />


        <button type="submit" class="btn">Submit</button>
    </fieldset>

    <div id="created-alert" class="alert alert-success" style="display:none; width:300px;">
        <a class="close" id="close-success" href="#">&times;</a>
        <p id="created"></p>
    </div>

    <div id="failed-alert" class="alert alert-error" style="display:none; width:300px;">
        <a class="close" id="close-failure" href="#">&times;</a>
        <p id="failed"></p>
    </div>
</form>

@section scripts {
    <script>
        $(document).ready(function () {


            // now we set some event handlers
            $('#close-success').click(function () {
                $('#created-alert').hide();
                return false;
            });

            $('#close-failure').click(function () {
                $('#failed-alert').hide();
                return false;
            });

            $('#recordForm').submit(function () {


                //compose an ACL object to post with the new record
                var acl = {
                    "FunctionEnum": "RecordAccess",
                    "FunctionProfiles": {
                        "DestroyRecord": {
                            "Setting": "Private",
                            "ReferenceStyle": "NoRefNoCopy",
                            "AccessLocations": [
                                {
                                    "FindBy": "me"
                                }
                            ]
                        }
                    }
                }


                var action = $("#recordForm").attr('action');


                var formData = {
                    'RecordTitle': $("#recordForm :input[name=RecordTitle]").val(),
                    'RecordRecordType': $("#recordForm :input[name=RecordRecordType]").val(),
                    'AccessControlList': acl                                        
                }

                $.ajax({
                    url: action,
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(formData)

                })
                .done(function (response, statusText) {
                    if (statusText === "success") {
                        $("#created").html("Record named: " + response.Results[0].RecordTitle.Value + " was created.");
                        $('#created-alert').show();
                    }
                })
                .fail(function (xhr) {
                    var err = eval("(" + xhr.responseText + ")");

                    $("#failed").html(err.ResponseStatus.Message);
                    $('#failed-alert').show();
                });
                return false;
            });




            $("#uri-select").change(function (evt) {
                setFieldValues($(this).attr('value'));
            });
        });
    </script>
}
